"""
AI-SOES URL配置
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.http import JsonResponse
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView

# 健康检查视图
def health_check(request):
    """系统健康检查"""
    return JsonResponse({
        'status': 'healthy',
        'message': 'AI-SOES is running',
        'version': '1.0.0'
    })

def api_info(request):
    """API信息"""
    return JsonResponse({
        'name': 'AI-SOES API',
        'version': '1.0.0',
        'description': '智能在线考试系统API',
        'docs': {
            'swagger': '/api/v1/docs/',
            'redoc': '/api/v1/redoc/',
            'schema': '/api/v1/schema/',
        }
    })

urlpatterns = [
    # 管理后台
    path('admin/', admin.site.urls),

    # API文档
    path('api/v1/schema/', SpectacularAPIView.as_view(), name='schema'),
    path('api/v1/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    path('api/v1/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),

    # API信息
    path('api/v1/', api_info, name='api-info'),

    # 系统健康检查
    path('api/v1/health/', health_check, name='health-check'),

    # 应用模块API
    path('api/v1/auth/', include('apps.users.urls')),
    path('api/v1/questions/', include('apps.question_bank.urls')),
    path('api/v1/exam-papers/', include('apps.exam_paper.urls')),
    path('api/v1/examinations/', include('apps.examination.urls')),
    # path('api/v1/grading/', include('apps.grading.urls')),  # 临时禁用，编码问题待修复
    # path('api/v1/ai/', include('apps.ai_integration.urls')),  # 临时禁用，编码问题待修复
]

# 开发环境下服务静态文件
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

    # Debug toolbar
    if 'debug_toolbar' in settings.INSTALLED_APPS:
        import debug_toolbar
        urlpatterns = [
            path('__debug__/', include(debug_toolbar.urls)),
        ] + urlpatterns

# 自定义错误处理
handler404 = 'config.views.custom_404'
handler500 = 'config.views.custom_500'